//
//  XMMeController.m
//  Second
//
//  Created by Alex on 16/7/13.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "XMMeController.h"

@interface XMMeController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIImagePickerController *imagePicker;
@property (nonatomic,strong)UIImageView *infoImageView;
@property (nonatomic,assign)NSInteger clickNumber;   //设置btn点击次数
@end

@implementation XMMeController

- (UIImageView *)infoImageView{
    
    if (!_infoImageView) {
        
        _infoImageView = [[UIImageView alloc]init];
    }
    return _infoImageView;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight)];
        self.tableView.dataSource = self;
        self.tableView.delegate   = self;
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MeCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"Me_cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ListCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"List_cell"];
    
    
    //调用本地相机
    self.imagePicker = [[UIImagePickerController alloc]init];
    self.imagePicker.delegate = self;
    //设置点击次数为1
    self.clickNumber = 1;

    
}
//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 1;
    }else{
        
        return 6;
    }
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        return 200;
    }else{
        
        return 50;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        MeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Me_cell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.imageBtn.layer.masksToBounds = YES;
        cell.imageBtn.layer.cornerRadius  = 35;
        
        [cell.imageBtn addTarget:self action:@selector(changeUserImage) forControlEvents:UIControlEventTouchUpInside];
        [cell.loginBtn addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
        
        if (self.clickNumber == 1) {
            
            [cell.imageBtn setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];
            self.clickNumber = 2;
        }else if (self.clickNumber == 2){
            
            [cell.imageBtn setBackgroundImage:self.infoImageView.image forState:UIControlStateNormal];
        }
        
         return cell;
        
    }else {
        
        ListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"List_cell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.listImage.layer.masksToBounds = YES;
        cell.listImage.layer.cornerRadius  = 15;
        
        NSArray *array = @[@"我发布的",@"我卖出的",@"我买过的",@"我赞过的",@"我的收藏",@"设置"];
        cell.listLabel.text = array[indexPath.row];
       
        return cell;
    }
    return nil;
    
}
//登录
- (void)loginAction{
    
    RegisterViewController *registerVC= [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"LoginViewController"];
    [self.navigationController presentViewController:registerVC animated:YES completion:nil];
}

#pragma mark-------调用系统相册实现方法
- (void)changeUserImage{
    
    _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:_imagePicker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *sourceImage = info[UIImagePickerControllerOriginalImage];
    self.infoImageView.image = sourceImage;
      [picker dismissViewControllerAnimated:YES completion:^{
    
        [self.tableView reloadData];
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 2) {
        
        XMSaleViewController *sale = [[XMSaleViewController alloc]init];
        [self.navigationController pushViewController:sale animated:YES];
    } else if (indexPath.row == 5) {
        
        XMSetViewController *setVC = [[XMSetViewController alloc]init];
        [self.navigationController pushViewController:setVC animated:YES];
        
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
